import request from '../../common/request'
import {hdb,xzl} from '../../common/config'

Page({

    data: {
      list:[]
    },

    onLoad: function () {
      this.getCollectList()
    },

    // 获取收藏列表
    async getCollectList(){
      const token = wx.getStorageSync('cookie');

      const URL = `${xzl}/getMyCollectGoods?access_token=${token}`;
      const res = await request(URL);

      if(res.code!==200){
        console.log('状态码不对',res);
        return;
      }

      this.setData({
        list:res.data
      })

    },

    // 取消收藏
    cancelCollect(e){
      const token = wx.getStorageSync('cookie');
      let {list} = this.data;
      const that = this;
      console.log('取消收藏',e.currentTarget.dataset.goodsid);
      const URL = `${hdb}?access_token=${token}`;
      // 取消收藏
      request(URL,'DELETE',JSON.stringify({goods_id:e.currentTarget.dataset.goodsid})).then(res=>{
        // 删除列表里相应的数据
        const index=list.findIndex(item=>item.goodsId===e.currentTarget.dataset.goodsid);
        list.splice(index,1);
        that.setData({
          list
        })
      }).catch(err=>{
        console.log(err)
      })

    },

    // 跳转到商品详情页
    navToGoodsDetail(e){
      const good_id = e.currentTarget.dataset.goodsid;
      wx.navigateTo({
        url: `/pages/goods_detail/goods_detail?goodsId=${good_id}`,
      });
        
    },


    // 跳转要商家主页
    navToSellerHome(e){
      const dataSet = e.currentTarget.dataset;
      wx.navigateTo({
        url: `/pages/sellerHome/sellerHome?userId=${dataSet.id}&avatarUrl=${dataSet.avatarurl}&nickName=${dataSet.nickname}`,
      });
    }
})